//
//  FamilyChangeView.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/2.
//  Copyright © 2020 tym. All rights reserved.
//

#import "FamilyChangeView.h"
#import "UIView+ZFFrame.h"
#import "FamilyChangCell.h"
#import "FamilyService.h"
#import "UserModel.h"
#import "FamilyModel.h"
#import "TYMBaseTableView.h"
@interface FamilyChangeView()

@property(nonatomic,strong) FamilyService *service;

@property(nonatomic,weak) IBOutlet UIView *conView;
@property(nonatomic,weak) IBOutlet UIButton *manageBtn;
@property(nonatomic,weak) IBOutlet TYMBaseTableView *tableView;

@property(nonatomic,strong) NSArray *dataArray;
@end

@implementation FamilyChangeView

-(FamilyService*)service{
    if (_service == nil) {
        _service = [[FamilyService alloc] init];
    }
    return _service;
}

-(void)getCurrentUserFamily{
    TYMWeak(self);
    [self.service getFindFamilysCompletion:^(NSError * _Nonnull error, NSArray<FamilyModel *> * _Nonnull array) {
        weakself.dataArray = array;
        [weakself.tableView reloadData];
    }];
}

-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.conView.zf_width = ScreenWidth;
    [self.conView showTopCircularDead];
    self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.manageBtn.layer.borderColor = rgba(151, 151, 151).CGColor;
    self.manageBtn.layer.borderWidth = 1;
    self.manageBtn.layer.cornerRadius = 5;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FamilyChangCell class]) bundle:nil] forCellReuseIdentifier:[FamilyChangCell identifier]];
    
    [self getCurrentUserFamily];
}

-(IBAction)familyManageAction:(id)sender{
    [self removeFromSuperview];
    if (self.showFamilyChangeActionBlock) {
        self.showFamilyChangeActionBlock(-1);
    }
}

-(IBAction)cancelAction:(id)sender{
    [self removeFromSuperview];
}

#pragma  mark - tableView 代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [FamilyChangCell cellForHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FamilyChangCell *cell = [tableView dequeueReusableCellWithIdentifier:[FamilyChangCell identifier] forIndexPath:indexPath];
    cell.familyModel = self.dataArray[indexPath.row];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    FamilyModel *model = self.dataArray[indexPath.row];
    if (model.familyId == [UserTool getCurrentFamily].familyId) {
        return;
    }
    
    [self.service getSwitchoverFamilyWithFamilyID:model.familyId Completion:^(LMJBaseResponse * responsed) {
        if (responsed.code == 0) {
            [self removeFromSuperview];
            [UserTool setFamily:model completion:^(BOOL state) {
                if (self.showFamilyChangeActionBlock) {
                    self.showFamilyChangeActionBlock(1);
                }
            }];
        }
    } ];
}


//-1:家庭管理。其它的事数组索引
+(void)showFamilyChangeViewcompletion:(void(^)(NSInteger actionType))completion{
    
    FamilyChangeView *view = [[[NSBundle mainBundle] loadNibNamed:@"FamilyChangeView" owner:self options:nil] lastObject];
    if (completion) {
        view.showFamilyChangeActionBlock = completion;
    }
    
    UIWindow *keyWindow = [[[UIApplication sharedApplication] windows] firstObject];
    view.frame = keyWindow.bounds;
    [keyWindow addSubview:view];
}

@end
